import javax.swing.*;
import java.beans.*;
import java.util.Observer;
import java.util.Observable;
import java.sql.*; 
import java.lang.*;
import java.util.*;
public class FichaMaterial extends Observable implements Observer {
    
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    
    M_Material material;
    M_Volumen volumen;
    JFrame ventanaFichaMaterial;
    AgregarVolumen agregarVolumen;
    FichaVolumen fichaVolumen;
    int identificador;
    int numeroVolumenes;
    int identificadoresVolumenes[];
    Object volumenes[][];
    Vector listaVolumenes;
    Vector vacio;
    boolean editando;
    
    public FichaMaterial(int id) {
        volumenes=new Object[500][8];
        volumen=new M_Volumen();
        material=new M_Material();
        listaVolumenes=new Vector();
        vacio=new Vector();
        identificador=id;
        levantaVista();
        traeDatos(id);
    	editando = false;
        llenaLista();
   }
    
    private void levantaVista(){
        ventanaFichaMaterial=new JFrame();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jComboBox1 = new javax.swing.JComboBox();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();

        ventanaFichaMaterial.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        ventanaFichaMaterial.setTitle("Sistema de administraci\u00f3n.... - Ficha del material ");
        ventanaFichaMaterial.setResizable(false);
        jLabel1.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Ficha");

        jLabel3.setText("Nombre:");

        jLabel4.setText("Autor:");

        jLabel6.setText("Tipo del material:");

        jLabel7.setText("Clasificacion o tematica:");

        jTextField3.setEnabled(false);

        jTextField4.setEnabled(false);

        jComboBox1.setEnabled(false);
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        //jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "915, enciclopedias de consulta" }));
        jComboBox2.setEnabled(false);

        jLabel5.setText("Codigo de identificacion del material:");

        jTextField1.setEnabled(false);

        jButton1.setText("Editar Datos");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Volumenes Disponibles", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11)));
        jScrollPane2.setViewportView(jList1);

        jScrollPane1.setViewportView(jScrollPane2);

        jButton2.setText("Ver ficha del volumen seleccionado");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Volver");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setEnabled(false);
        jScrollPane3.setViewportView(jTextArea1);

        jLabel2.setText("Descripcion");

        jTextField2.setEnabled(false);

        jLabel8.setText("Titulo:");

        jLabel9.setText("Copias Prestadas:");

        jLabel10.setText("0");

        jLabel11.setText("Copias Disponibles:");

        jLabel12.setText("0");

        jButton4.setText("Agregar Volumen");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(ventanaFichaMaterial.getContentPane());
        ventanaFichaMaterial.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 457, Short.MAX_VALUE)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 457, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel9)
                            .add(jLabel11))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(jLabel12)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 262, Short.MAX_VALUE)
                                .add(jButton1))
                            .add(jLabel10)))
                    .add(layout.createSequentialGroup()
                        .add(jButton2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 143, Short.MAX_VALUE)
                        .add(jButton4))
                    .add(jButton3)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel4)
                            .add(jLabel8)
                            .add(jLabel7)
                            .add(jLabel2)
                            .add(jLabel6)
                            .add(jLabel3)
                            .add(jLabel5))
                        .add(26, 26, 26)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jTextField3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
                            .add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
                            .add(jComboBox2, 0, 256, Short.MAX_VALUE)
                            .add(jTextField2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
                            .add(jTextField4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jComboBox1, 0, 256, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .add(17, 17, 17)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel6)
                    .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jTextField4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel4))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel8))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jLabel7)
                    .add(jComboBox2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jLabel2)
                    .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 35, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel9)
                    .add(jLabel10))
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(16, 16, 16)
                        .add(jButton1))
                    .add(layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel11)
                            .add(jLabel12))))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 115, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton2)
                    .add(jButton4))
                .add(18, 18, 18)
                .add(jButton3)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        ventanaFichaMaterial.pack();
        ventanaFichaMaterial.setVisible(true);
    }

//boton volver
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
		this.setChanged();
		this.notifyObservers(); 
		ventanaFichaMaterial.dispose();
    }

//boton agregar volumen
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
		ventanaFichaMaterial.setVisible(false);
		agregarVolumen= new AgregarVolumen(identificador);
		agregarVolumen.addObserver(this);
	
    }
    
//boton ver ficha volumen
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
		if(jList1.getSelectedIndex()!=-1){
			fichaVolumen = new FichaVolumen(identificadoresVolumenes[jList1.getSelectedIndex()],identificador);
			fichaVolumen.addObserver(this);
			ventanaFichaMaterial.setVisible(false);
	    }
    }

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
		compruebaTipoSeleccionado();
    }

//boton editar datos
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
 		
 		if(jButton1.getText().equals("Editar Datos")){
	 		editando = true;
	 		jTextField3.setEnabled(true);
	        jTextField4.setEnabled(true) ;
	        jComboBox1.setEnabled(true) ;
	        jComboBox2.setEnabled(true) ;
	        
	        jTextArea1.setEnabled(true) ;
	        jTextField2.setEnabled(true); 
		    jButton1.setText("Guardar cambios");
		    jButton2.setEnabled(false);
		    jButton4.setEnabled(false);
			System.out.println("editando");
		}else 
		
		if(jButton1.getText().equals("Guardar cambios")){
			editando = false;
			editarDatos();
			jTextField3.setEnabled(false);
	        jTextField4.setEnabled(false) ;
	        jComboBox1.setEnabled(false) ;
	        jComboBox2.setEnabled(false) ;
	        
	        jTextArea1.setEnabled(false) ;
	        jTextField2.setEnabled(false); 
			jButton1.setText("Editar Datos");
			jButton2.setEnabled(true);
			jButton4.setEnabled(true);
		}
	}
  
  
	public void update( Observable obse,Object obje ) {
          	
       	ventanaFichaMaterial.setVisible(true);
       
    }
      
    private void traeDatos(int identificador){
	   	material.getDatosMaterial(identificador);
	   	
	   	jTextField1.setText(material.mat_cod_string);
	   	jTextField2.setText(material.mat_titulo);
	   	jTextField3.setText(material.mat_nombre);
	   	jTextField4.setText(material.mat_autor);
	   	jTextArea1.setText(material.mat_descripcion);
	   	jLabel10.setText(material.mat_prestados_string);
	   	jLabel12.setText(material.mat_disponibles_string);
	   	
	   	
	    llenaComboBox(material.mat_tematica, material.mat_tipo);
	    if(material.esPrestable==false){
	    	jLabel10.setText("");
	    	jLabel9.setText("Este material no puede ser prestado");
	    }
   }   
   
  	private void editarDatos(){
   	material.editarDatos(material.mat_cod,material.idTiposMateriales[jComboBox1.getSelectedIndex()], material.idTematicasMateriales[jComboBox2.getSelectedIndex()], jTextField3.getText(), jTextField2.getText(), jTextField4.getText(), jTextArea1.getText());
   }
   
	private void llenaComboBox(int idTematica, int idTipo){
		int i;
		int j;
		int si=0;
		int sj=0;
		try{
			material.getTiposMateriales();
			material.getCategorias();
			for(i=0;i<material.numeroTiposMateriales;i++){
		    	jComboBox1.insertItemAt(material.tiposMateriales[i],i);
		    	if(idTipo==material.idTiposMateriales[i]){
		    		si=i;
		    		System.out.println("TRUE!!!");	
		    	}
		    }
		    jComboBox1.setSelectedIndex(si);
		    for(j=0;j<material.numeroTematicasMateriales;j++){
		    	jComboBox2.insertItemAt(material.tematicasMateriales[j],j);
		    	if(idTematica==j)
		    		if(idTipo==material.idTematicasMateriales[j]){
		    			sj=j;
		    		}
		    }
		    jComboBox2.setSelectedIndex(sj);
	    }catch(Exception e){}
	}
	
	private void llenaLista(){
	
		generaResultados();
		if(numeroVolumenes>0)
		jList1.setListData(listaVolumenes);
			
	}
	
	private void generaResultados(){
		volumen.getVolumenes(identificador);
		volumenes=volumen.listaVolumenes;
		identificadoresVolumenes=new int[volumen.numeroVolumenes];
		String cadenaResultado;
	    numeroVolumenes=volumen.numeroVolumenes;
		System.out.println("numero de volumenes="+numeroVolumenes);
		for (int i=0; i<numeroVolumenes;i++){
			cadenaResultado ="Codigo= "+volumenes[i][1]+" | Ubicacion= "+volumenes[i][6]+" | Estado Fisico= "+volumenes[i][2]+" | Disponibilidad= "+volumenes[i][3]+" ";
			identificadoresVolumenes[i]=(Integer) volumenes[i][1];
			listaVolumenes.addElement(cadenaResultado);
		}
	
	}

	public void compruebaTipoSeleccionado(){
		if(editando==true){
			if(material.tiposMaterialesInfo[jComboBox1.getSelectedIndex()] ==0){
				jTextField2.setEnabled(false);
				jTextField4.setEnabled(false);
				jComboBox2.setEnabled(false);
				jTextField2.setText("");
				jTextField4.setText("");
			}
			if(material.tiposMaterialesInfo[jComboBox1.getSelectedIndex()] ==1){
				jTextField2.setEnabled(true);
				jTextField4.setEnabled(true);
				jComboBox2.setEnabled(true);
			}
		}	   
	}


}